package com.java.Lock.ReentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockAPI {
    class ThreadSon extends Thread {
        @Override
        public void run() {
            System.out.println("线程(" + Thread.currentThread().getName() + ")开始");

            for (int i = 0; i < 5; i++) {
                System.out.println("线程(" + Thread.currentThread().getName() + ")执行中");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println("线程(" + Thread.currentThread().getName() + ")结束");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // 创建对象：默认为不公平锁，参数为true则设为公平锁。
        Lock lock1 = new ReentrantLock();
        Lock lock2 = new ReentrantLock(true);

        // lock：拿走钥匙关锁。拿到锁的线程不能被中断

        // lockInterruptibly：拿走钥匙关锁。拿到锁的线程不能被中断
        lock1.lockInterruptibly();

        // tryLock：尝试拿走钥匙关锁，返回结果表示能否成功拿走钥匙关锁
        // tryLock(long time, TimeUnit unit

        // isLocked：获取锁状态

        // unlock：归还钥匙开锁


    }
}
